#pragma once


#include<iostream>
#include<assert.h>
#include<utility>

using namespace std;

namespace RBTree
{
	enum Colour
	{
		RED,
		BLACK
	};
	template<class K, class V>
	class RBTreeNode
	{
	public:
		RBTreeNode<K, V>* _left;
		RBTreeNode<K, V>* _right;
		RBTreeNode<K, V>* _parent;
		pair<K, V> _kv;
		Colour _col;

		RBTreeNode(const pair<K, V>& kv)
			:_left(nullptr), _right(nullptr), _parent(nullptr), _col(RED), _kv(kv);
		{}
	};

	template<class K, class V>
	class RBTree
	{
		typedef RBTreeNode<K, V> Node;
	public:
		bool is_balance()
		{
			return is_balance(_root);
		}

		bool Checknum(Node* root, int blacknum, int stdnum)
		{
			if (root == nullptr)
			{
				if (blacknum != stdnum)
				{
					return false;
				}
				return true;
			}

			if (root->_col == BLACK)
			{
				++blacknum;
			}

			return Checknum(root->_left, blacknum, stdnum)
				&& Checknum(root->_right, blacknum, stdnum);
		}
		bool is_balance(Node* root)
		{
			if (root == nullptr)
			{
				return true;
			}

			if (root->_col == RED)
			{
				return false;
			}
			int stdnum = 0;
			Node* cur = root;
			while (cur->_left)
			{
				if (cur->_col == BLACK)
				{
					++stdnum;
				}
				cur = cur->_left;
			}

			return Checknum(root, 0, stdnum);
		}
		bool insert(const pair<K, V>& kv)
		{
			if (_root == nulptr)
			{
				_root = new Node(kv);
				_root->_col = BLACK;
				return true;
			}
			Node* parent = nullptr;
			Node* cur = _root;
			while (cur)
			{
				if (kv.first > cur->_kv.first)
				{
					cur = cur->_right;
				}
				else if (kv.first < cur->_kv.first)
				{
					cur = cur->_left;
				}
				else
				{
					return fasle;
				}
				parent = cur;
			}

			cur = new Node(kv);

			if (parent->_left = cur)
			{
				parent->_left = cur
			}
			else
			{
				parent->_right = cur;
			}
			cur->_col = RED;
			cur->_parent = parent;

			while (parnet && parent->_col == RED)
			{
				Node* grandparent = parent->_parent;
				if (parent == grandparent->_left)
				{
					Node* uncle = grandparent->_right;
					if (uncle && uncle->_col == RED)
					{
						parent->_col = uncle->_col = BLACK;
						grandparent->_col = RED;
						cur = grandparent;
						parent = cur->_parent;
					}
					else//uncle is not or black
					{
						if (cur = parent->_left)
						{
							RotateR(grandparent);
							grandparent->_col = RED;
							parent->_col = BLACK;
						}
						else
						{
							RotateL(parent);
							RotateR(grandparent);
							grandparent->_col = RED;
							cur->_col = BLACK;

						}
						break;
					}
				}
				else//parent == grandparent->_right
				{
					Node* uncle = grandparent->_left;
					if (uncle && uncle->_col == RED)
					{
						RotateL(grandparent);
						grandparent->_col = RED;
						parent->_col = BLACK;
					}
					else
					{
						RotateL(parent);
						RotateL(grandparent);
						cur->_col = BLACK;
						grandparent->_col = RED;
					}
					break;
				}
			}
			return true;
		}

		void RotateR(Node* parent)
		{
			Node* cur = parent->_left;
			Node* curright = cur->_right;

			parent->_left = curright;
			if (curright)
			{
				curright->_parent = parent;
			}

			cur->_left = parent;
			Node* ppnode = parent->_parent;
			parent->_parent = cur;

			if (ppnode == nullptr)
			{
				_root = cur;
				cur->_parent = nullptr;
			}
			else
			{
				if (ppnode->_left = parent)
				{
					ppnode->_left = cur;
				}
				else
				{
					ppnode->_right = cur;
				}
				cur->_parent = ppnode;
			}

		}
		void RotateL(Node* parent)
		{

			Node* cur = parent->_right;
			Node* curleft = cur->_left;
			parent->_right = curleft;
			if (curleft)
			{
				curleft->_parent = parent;
			}
			cur->_left = parent;
			Node* ppnode = parent->_parent;
			parent->_parent = cur;
			if (parent == _root)
			{
				_root = cur;
				cur->_parent = nullptr;
			}
			else
			{
				if (ppnode->_left == parent)
				{
					ppnode->_left = cur;
				}
				else
				{
					ppnode->_right = cur;
				}
				cur->_parent = ppnode;
			}
		}

	private:
		Node* _root = nullptr;
	};


}